package com.fsnbigdata.service.impl;
import com.fsnbigdata.domain.PlantConservation;
import com.fsnbigdata.domain.dto.NamingPair2;
import com.fsnbigdata.repository.EndangeredSpeciesImageRepository;
import com.fsnbigdata.service.api.EndangeredSpeciesService;
import com.fsnbigdata.util.ObjectNull;
import com.fsnbigdata.vo.NewClass;
import com.querydsl.core.types.Predicate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.roo.addon.layers.service.annotations.RooServiceImpl;
import com.fsnbigdata.domain.EndangeredSpecies;
import com.fsnbigdata.repository.EndangeredSpeciesRepository;
import io.springlets.data.domain.GlobalSearch;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 * = EndangeredSpeciesServiceImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooServiceImpl(service = EndangeredSpeciesService.class)
@Service
@Transactional(readOnly = true)
public class EndangeredSpeciesServiceImpl implements EndangeredSpeciesService {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    private EndangeredSpeciesRepository endangeredSpeciesRepository;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Autowired
    private EndangeredSpeciesImageRepository endangeredSpeciesImageRepository;

    /**
     * TODO Auto-generated constructor documentation
     *
     * @param endangeredSpeciesRepository
     */
    @Autowired
    public EndangeredSpeciesServiceImpl(EndangeredSpeciesRepository endangeredSpeciesRepository) {
        setEndangeredSpeciesRepository(endangeredSpeciesRepository);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return EndangeredSpeciesRepository
     */
    public EndangeredSpeciesRepository getEndangeredSpeciesRepository() {
        return endangeredSpeciesRepository;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param endangeredSpeciesRepository
     */
    public void setEndangeredSpeciesRepository(EndangeredSpeciesRepository endangeredSpeciesRepository) {
        this.endangeredSpeciesRepository = endangeredSpeciesRepository;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param endangeredSpecies
     */
    @Transactional
    public void delete(EndangeredSpecies endangeredSpecies) {
        getEndangeredSpeciesRepository().delete(endangeredSpecies);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param entities
     * @return List
     */
    @Transactional
    public List<EndangeredSpecies> save(Iterable<EndangeredSpecies> entities) {
        return getEndangeredSpeciesRepository().save(entities);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     */
    @Transactional
    public void delete(Iterable<Long> ids) {
        List<EndangeredSpecies> toDelete = getEndangeredSpeciesRepository().findAll(ids);
        getEndangeredSpeciesRepository().deleteInBatch(toDelete);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param entity
     * @return EndangeredSpecies
     */
    @Transactional
    public EndangeredSpecies save(EndangeredSpecies entity) {
        return getEndangeredSpeciesRepository().save(entity);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     * @return EndangeredSpecies
     */
    public EndangeredSpecies findOne(Long id) {
        return getEndangeredSpeciesRepository().findOne(id);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     * @return EndangeredSpecies
     */
    public EndangeredSpecies findOneForUpdate(Long id) {
        return getEndangeredSpeciesRepository().findOneDetached(id);
    }

    @Override
    public Page<EndangeredSpecies> search(Predicate predicate, Pageable pageable) {
        return getEndangeredSpeciesRepository().findAll(predicate, pageable);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @return List
     */
    public List<EndangeredSpecies> findAll(Iterable<Long> ids) {
        return getEndangeredSpeciesRepository().findAll(ids);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return List
     */
    public List<EndangeredSpecies> findAll() {
        return getEndangeredSpeciesRepository().findAll();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public long count() {
        return getEndangeredSpeciesRepository().count();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<EndangeredSpecies> findAll(GlobalSearch globalSearch, Pageable pageable) {
        return getEndangeredSpeciesRepository().findAll(globalSearch, pageable, null);
    }

    @Override
    public Page<EndangeredSpecies> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        return getEndangeredSpeciesRepository().findAll(globalSearch, pageable, creator);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<EndangeredSpecies> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        return getEndangeredSpeciesRepository().findAllByIdsIn(ids, globalSearch, pageable);
    }

    @Override
    public List<NamingPair2> findAllByInitial(String initial) {
        return getEndangeredSpeciesRepository().findAllByInitial(initial);
    }

    @Override
    public List getPageList(String name, String latin, Pageable pageable1) {
        List list = new ArrayList(  );
        list.add( endangeredSpeciesRepository.queryCountByKey( name,latin ) );
        List<NewClass> endangereds = new ArrayList <>(  );
        for (Object o:endangeredSpeciesRepository.queryAllByKey( name,latin,pageable1 )){
            Object[] objects = (Object[]) o;
            NewClass newClass = new NewClass();
            newClass.setId( ""+objects[0] );
            newClass.setName(ObjectNull.takeOutNull(objects[1]) );
            newClass.setLatin_name( ObjectNull.takeOutNull(objects[2]) );
            newClass.setLocation( ObjectNull.takeOutNull(objects[3]) );
            newClass.setCreator( ObjectNull.takeOutNull(objects[4]) );
            newClass.setInitial( ObjectNull.takeOutNull(objects[5]) );
            newClass.setDisturbances(ObjectNull.takeOutNull(objects[6]));
            List<String> url = endangeredSpeciesImageRepository.getUrl( ""+objects[0] );
            newClass.setUrl( url.size()>0?url.get( 0 ):null );
            newClass.setNumber( ""+objects[0] );
            endangereds.add( newClass );
        }
        list.add( endangereds );
        return list;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Class
     */
    public Class<EndangeredSpecies> getEntityType() {
        return EndangeredSpecies.class;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Class
     */
    public Class<Long> getIdType() {
        return Long.class;
    }
}
